package com.cloudcc.mobile.network;

import java.util.Calendar;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;

import com.cloudcc.mobile.AppContext;
import com.cloudcc.mobile.entity.AddImageInfo;
import com.cloudcc.mobile.entity.EventTaskEntity;
import com.cloudcc.mobile.entity.addmicropost.AddMicroPostDMany;
import com.cloudcc.mobile.entity.addmicropost.AddMicroPostF;
import com.cloudcc.mobile.entity.map.MapLocation;
import com.cloudcc.mobile.manager.UserManager;
import com.cloudcc.mobile.parser.RenWuParse;
import com.cloudcc.mobile.parser.ReturnCodeParser;
import com.cloudcc.mobile.service.RenWuService;
import com.cloudcc.mobile.util.Json;
import com.cloudcc.mobile.util.NotificationUtils;
import com.cloudcc.mobile.util.Tools;
import com.cloudcc.mobile.view.login.LoginNewActivity;
import com.litesuits.android.log.Log;
import com.mypage.utils.SaveTemporaryData;

public class QianDaoNewWork {
	private Context context;
	private int num;
	private String eventId;
	private String text, name, key, id;
	private List<AddImageInfo> list;
	private String theme;
	private MapLocation location = new MapLocation();

	public QianDaoNewWork(Context context, int num) {
		this.num = num;
		this.context = context;
	}

	public void QiaoDaoChat(MapLocation location, String id, String name,
			List<AddImageInfo> list, String text, String key, String theme) {
		try {
			this.list = list;
			this.location = location;
			this.text = text;
			this.name = name;
			this.key = key;
			this.id = id;
			this.theme = theme;
			NotificationUtils.showNotification(context, "发送中...", "微贴",
					"微贴发送中...", num);
			EventTaskEntity renwu = new EventTaskEntity();
			renwu.setBelongtoid(UserManager.getManager().getUser().userId);
			renwu.setBelongtoidccname(UserManager.getManager().getUser().userName);
			renwu.setOwnerid(UserManager.getManager().getUser().userId);
			if (key.equals("2")) {
				renwu.setIstask("1");
				renwu.setExpiredate(Tools.getYMDTime(System.currentTimeMillis()));
			} else {
				renwu.setIstask("0");
				String begintime = Tools
						.getTime(System.currentTimeMillis() - 600000);
				String endtime = Tools
						.getTime(System.currentTimeMillis() - 500000);
				renwu.setBegintime(begintime);
				renwu.setEndtime(endtime);
			}
			if (!id.equals("null")) {
				String ids = id.substring(0, 3);
				if (ids.equals("003") || ids.equals("004")) {// 联系人、潜在客户 名称
					renwu.setWhoid(id);
					renwu.setWhoobj(ids);
				} else {// 客户 相关项
					renwu.setRelateid(id);
					renwu.setRelateobj(ids);
				}
				renwu.setRemark(text);
			} else {
				renwu.setRemark(text);
			}
			renwu.setSubject(name);
			Log.d("shifoucongzheli", name + "renwu");
			renwu.setIsemailnotification("true");
			renwu.setIsremider("true");
			renwu.setType(theme);
			// String
			// begintime=Tools.getYMDTime(System.currentTimeMillis())+" "+getTime1();
			// String
			// endtime=Tools.getYMDTime(System.currentTimeMillis())+" "+getTime2();
			renwu.setName(theme);
			renwu.setIscompleted("1");
			renwu.setStatus("已结束");
			if (location != null) {
				renwu.setPosition(location.getAddress());
			}
			String jsonstring = Json.toJson(renwu);
			Tools.i("login", "shijian-名片----" + jsonstring);
			new MyTask().execute(jsonstring);
		} catch (Exception e) {
			Tools.handle(e);
		}
	}
	public void QiaoDaoChatputong(MapLocation location, String id, String name,
			List<AddImageInfo> list, String text, String key, String theme) {
	
			this.list = list;
			this.location = location;
			this.text = text;
			this.name = name;
			this.key = key;
			this.id = id;
			this.theme = theme;
		if (!"No".equals(SaveTemporaryData.mBeauInfoTui)){
			NotificationUtils.showNotification(context, "发送中...", "微贴",
					"微贴发送中...", num);
			SaveTemporaryData.mBeauInfoTui = "";
		}

			EventTaskEntity renwu = new EventTaskEntity();
			renwu.setBelongtoid(UserManager.getManager().getUser().userId);
			renwu.setBelongtoidccname(UserManager.getManager().getUser().userName);
			renwu.setOwnerid(UserManager.getManager().getUser().userId);
//			eventId = RenWuParse.getEventId(result);
			if (list.size() > 0) {
				AddMicroPostDMany addMicroPostDMany = new AddMicroPostDMany();
				if (location != null) {
					addMicroPostDMany.setLatitude(location
							.getLatitude());
					addMicroPostDMany.setLongitude(location
							.getLongitude());
					addMicroPostDMany.setAddress(location.getAddress());
				}
				addMicroPostDMany.setBody(text);
				addMicroPostDMany.setTargetId(id);// 从签到进去的传 事件id
													// 详情进去的传详情id
//				addMicroPostDMany.setTaskIdOrEventId(eventId);
				addMicroPostDMany.setTargetType("record");
				AppContext.ADDMICROPOST = "addMicroPostDMany";
				AddMicroPostUtil send = new AddMicroPostUtil(context,
						num);
				send.sendWeiTie(addMicroPostDMany, list, false);
			} else {
				AddMicroPostF addMicroPostF = new AddMicroPostF();
				if (location != null) {
					addMicroPostF.setAddress(location.getAddress());
					addMicroPostF.setLatitude(location.getLatitude());
					addMicroPostF.setLongitude(location.getLongitude());
				} else {
					addMicroPostF.setAddress("");
				}
				AppContext.ADDMICROPOST = "addMicroPostF";
				addMicroPostF.setBody(text);
				addMicroPostF.setTargetId(id);// 从签到进去的传 事件id 详情进去的传详情id
				addMicroPostF.setTargetType("record");
//				addMicroPostF.setTaskIdOrEventId(eventId);
				AppContext.ADDMICROPOST = "addMicroPostF";
				AddMicroPostUtil send2 = new AddMicroPostUtil(context,
						num);
				send2.sendWeiTief(addMicroPostF, false);
			}
			
	}
	class MyTask extends AsyncTask<String, Void, String> {
		private AddMicroPostDMany addMicroPostDMany;
		private AddMicroPostF addMicroPostF;

		@Override
		protected String doInBackground(String... params) {
			String jsonstring = null;
			try {
				RenWuService service = new RenWuService();
				String data = params[0];
				if (key.equals("2")) {
					jsonstring = service.insertTask(data);
				} else {
					jsonstring = service.insertEvent(data);
				}
				Tools.i("aaaaaaa", jsonstring);
			} catch (Exception e) {
				Tools.handle(e);
			}
			return jsonstring;
		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			Log.d("fanhuishuju", result);
			try {
				if (Tools.isNull(result)) {
					NotificationUtils.clearNotification(context, num);
					Tools.showInfo(context, "网络异常，请稍后再试！");
					return;
				}
				if (result.equals("outTime")) {
					NotificationUtils.clearNotification(context, num);
					Tools.showInfo(context, "网络超时，请稍后再试！");
					return;
				}
				String code = ReturnCodeParser.getReturnCode(result);
				if (code.equals("-2")) {
					NotificationUtils.clearNotification(context, num);
					Intent intent = new Intent(context, LoginNewActivity.class);
					context.startActivity(intent);
					return;
				}
				boolean msg = RenWuParse.getUpResult(result);
				if (msg) {
					eventId = RenWuParse.getEventId(result);
					if (list.size() > 0) {
						addMicroPostDMany = new AddMicroPostDMany();
						if (location != null) {
							addMicroPostDMany.setLatitude(location
									.getLatitude());
							addMicroPostDMany.setLongitude(location
									.getLongitude());
							addMicroPostDMany.setAddress(location.getAddress());
						}
						addMicroPostDMany.setBody(text);
						addMicroPostDMany.setTargetId(id);// 从签到进去的传 事件id
															// 详情进去的传详情id
						addMicroPostDMany.setTaskIdOrEventId(eventId);
						addMicroPostDMany.setTargetType("record");
						AppContext.ADDMICROPOST = "addMicroPostDMany";
						AddMicroPostUtil send = new AddMicroPostUtil(context,
								num);
						send.sendWeiTie(addMicroPostDMany, list, false);
					} else {
						addMicroPostF = new AddMicroPostF();
						if (location != null) {
							addMicroPostF.setAddress(location.getAddress());
							addMicroPostF.setLatitude(location.getLatitude());
							addMicroPostF.setLongitude(location.getLongitude());
						} else {
							addMicroPostF.setAddress("");
						}
						AppContext.ADDMICROPOST = "addMicroPostF";
						addMicroPostF.setBody(text);
						addMicroPostF.setTargetId(id);// 从签到进去的传 事件id 详情进去的传详情id
						addMicroPostF.setTargetType("record");
						addMicroPostF.setTaskIdOrEventId(eventId);
						AppContext.ADDMICROPOST = "addMicroPostF";
						AddMicroPostUtil send2 = new AddMicroPostUtil(context,
								num);
						send2.sendWeiTief(addMicroPostF, false);
					}
				} else {
					Tools.showInfo(context, "签到失败，请稍后再试");
				}
			} catch (Exception e) {
				Tools.handle(e);
			}
		}

	}

	@SuppressLint("SimpleDateFormat")
	public String getTime1() {
		Calendar calendar = Calendar.getInstance();
		int hour = calendar.get(Calendar.HOUR_OF_DAY);
		hour = hour + 1;
		String Hour = hour + "";
		if (hour >= 24) {
			hour = hour - 24;
		}
		if (hour < 10) {
			Hour = "0" + hour;
		}
		String time = Hour + ":00:00";
		return time;
	}

	@SuppressLint("SimpleDateFormat")
	public String getTime2() {
		Calendar calendar = Calendar.getInstance();
		int hour = calendar.get(Calendar.HOUR_OF_DAY);
		hour = hour + 2;
		String Hour = hour + "";
		if (hour >= 24) {
			hour = hour - 24;
		}
		if (hour < 10) {
			Hour = "0" + hour;
		}
		String time = Hour + ":00:00";
		return time;
	}
}
